package com.honey.mybatis.generator.dao;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.Method;
import com.honey.compilation.xml.XmlElement;
import com.honey.core.compiler.CompilationException;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.notify.Signal;
import com.honey.mybatis.generator.AbstractMybatis;
import com.honey.mybatis.generator.AbstractProduceJavaCompilation;
import com.honey.mybatis.generator.MybatisPlugin;
import com.honey.mybatis.generator.dao.method.BaseDaoDelete1Method;
import com.honey.mybatis.generator.dao.method.BaseDaoDelete2Method;
import com.honey.mybatis.generator.dao.method.BaseDaoInsert1Method;
import com.honey.mybatis.generator.dao.method.BaseDaoInsert2Method;
import com.honey.mybatis.generator.dao.method.BaseDaoSelectList1Method;
import com.honey.mybatis.generator.dao.method.BaseDaoSelectList2Method;
import com.honey.mybatis.generator.dao.method.BaseDaoSelectListForSqlMethod;
import com.honey.mybatis.generator.dao.method.BaseDaoSelectOne1Method;
import com.honey.mybatis.generator.dao.method.BaseDaoSelectOne2Method;
import com.honey.mybatis.generator.dao.method.BaseDaoSelectOneForSqlMethod;
import com.honey.mybatis.generator.dao.method.BaseDaoUpdate1Method;
import com.honey.mybatis.generator.dao.method.BaseDaoUpdate2Method;
import com.honey.mybatis.generator.dependent.DependentModel;
import com.honey.mybatis.generator.inversion.InversionModel;

public class ProduceBaseDaoImplements extends AbstractProduceJavaCompilation {
	
	public ProduceBaseDaoImplements(MybatisPlugin mybatisPlugin,
			IntrospectSchema introspectSchema,
			IntrospectSchemaColumn introspectSchemaColumn) {
		super(mybatisPlugin, introspectSchema, introspectSchemaColumn);
	}
	
	private AbstractJavaCompilation abstractJavaCompilation ;
	
	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceCompilation()
	 */
	@Override
	public AbstractJavaCompilation produceCompilation() throws CompilationException {
		abstractJavaCompilation = new BaseDaoImplements(mybatisPlugin, introspectSchema, introspectSchemaColumn).createJavaCompilation();
		
		snedBeanSignalToSpringPlugin();
		return abstractJavaCompilation;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceFields()
	 */
	@Override
	public Field[] produceFields() throws CompilationException {
		
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see com.honey.mybatis.generator.AbstractProduceJavaCompilation#produceMethods()
	 */
	@Override
	public Method[] produceMethods() throws CompilationException {
		Method[] answer = new Method[]{
				new BaseDaoSelectList1Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoSelectOne1Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoInsert1Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoUpdate1Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoDelete1Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoSelectList2Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoSelectOne2Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoInsert2Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoUpdate2Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoDelete2Method(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoSelectOneForSqlMethod(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
				new BaseDaoSelectListForSqlMethod(null,mybatisPlugin, introspectSchema, introspectSchemaColumn).createMethod(),
		};
		return answer;
	}
	
	/**
	 * @return the abstractJavaCompilation
	 */
	public final AbstractJavaCompilation getAbstractJavaCompilation() {
		return abstractJavaCompilation;
	}
	
	protected void snedBeanSignalToSpringPlugin(){
		//向Spring插件发送配置bean的通知
		Signal signal = new Signal();
		XmlElement springBean = new XmlElement("bean");
		springBean.addAttribute("id", "baseMybatisdao");
		springBean.addAttribute("class", 
			new AbstractMybatis(mybatisPlugin ){
				public  String getImplementsType(){
					return super.getBaseDaoImplementsClassType(introspectSchema.getSchema(),null).getLongName(); 
				}
			}.getImplementsType()
		);
		if(mybatisPlugin.getDependentModel() == DependentModel.EXTEND)
			springBean.addAttribute("abstract", "true");
		
		
		XmlElement property = new XmlElement(springBean,"property");
		if( mybatisPlugin.getInversionModel() ==  InversionModel.EXTEND ){
			property.addAttribute("name","sqlSessionFactory").addAttribute("ref","sqlSessionFactory");
		}else{
			String name = mybatisPlugin.getInversionModel().inversionFiled(null)[0].getName();
			property.addAttribute("name",name).addAttribute("ref",name);
		}
		signal.setAttachment(springBean);
		super.snedSignal(signal);
	}
	
}
